package com.lwbldy.modules.student.controller;


import com.github.pagehelper.PageHelper;
import com.lwbldy.common.plugins.logging.annotation.Logging;
import com.lwbldy.common.plugins.logging.enums.BusinessType;
import com.lwbldy.common.plugins.repeat.annotation.RepeatSubmit;
import com.lwbldy.common.tools.PageUtils;
import com.lwbldy.common.tools.R;
import com.lwbldy.common.tools.StringUtils;
import com.lwbldy.modules.student.domain.PreLog;
import com.lwbldy.modules.student.domain.PreRegistration;
import com.lwbldy.modules.student.service.IPreRegistrationService;


import com.lwbldy.modules.student.service.MajorService;
import com.lwbldy.modules.student.service.impl.PreLogServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@Api(tags = "")
@RequestMapping("/student/preregistration")
public class PreRegistrationController {

    /**
         * Describe: 基础路径
         * */
    private static String MODULE_PATH = "student/preregistration/";


    @Resource
    private IPreRegistrationService preRegistrationService;

    @Resource
    private PreLogServiceImpl preLogService;

    @Resource
    private MajorService majorService;

    @GetMapping("main")
    @ApiOperation(value="获取列表视图")
    public String main(ModelMap map){
        map.put("majorList",majorService.queryList(null));
        return MODULE_PATH + "main";
    }



    @GetMapping("list")
    @ApiOperation(value="获取角色列表数据")
    @ResponseBody
    public R data(@RequestParam(value = "page", defaultValue = "1") Integer page,
                  @RequestParam(value = "limit", defaultValue = "15") Integer limit,
                  @RequestParam(value = "type",required = false) String type,
                  @RequestParam(value = "field",required = false) String field,
                  @RequestParam(value = "nameOrIdCard",required = false) String nameOrIdCard,
                  @RequestParam(value = "schoolId",required = false) Integer schoolId,
                  @RequestParam(value = "status",required = false) Integer status,
                  @RequestParam(value = "majorId",required = false) Integer majorId,
                  @RequestParam(value = "xueli",required = false) String xueli,
                  @RequestParam(value = "jfstatus",required = false) Integer jfstatus
    ){
        PageHelper.startPage(page, limit);

        PreRegistration preRegistration = null;
        if(nameOrIdCard != null || schoolId != null || status != null || majorId != null
                || jfstatus!=null || xueli != null){
            preRegistration = new PreRegistration();
            preRegistration.setNameOrIdCard(nameOrIdCard);
            preRegistration.setSchoolId(schoolId);
            preRegistration.setStatus(status);
            preRegistration.setMajorId(majorId);
            preRegistration.setJfStatus(jfstatus);
            preRegistration.setXueli(xueli);
        }

        if(field != null){
            String order = type != null ? " " + type : "";
            preRegistration.setOrderByClause("`pre_registration`.`score`" + order);
        }

        List<PreRegistration> brandList = preRegistrationService.selectList(preRegistration);
        return PageUtils.restPage(brandList);
    }

    @DeleteMapping("remove/{id}")
    @ApiOperation(value="删除数据")
    @ResponseBody
    public R remove(@PathVariable int id){
        int result  = preRegistrationService.deleteById(id);
        return R.resultRow(result);
    }

    @DeleteMapping("batchRemove/{ids}")
    @ApiOperation(value="批量删除数据")
    @ResponseBody
    public R batchRemove(@PathVariable String ids){
        int result = preRegistrationService.deleteByIds(StringUtils.StringToIntArr(ids.split(",")).toArray());
        return R.resultRow(result);
    }

    @GetMapping("payStudent/{id}")
    @ApiOperation(value="获取新增视图")
    public String payStudent(@PathVariable int id, ModelMap map){
        map.put("id",id);
        return MODULE_PATH + "payStudent";
    }


//    @PutMapping("putStudent/{id}")
//    @ApiOperation(value="推送学生到博思")
//    @ResponseBody
//    public R puhStudent(@PathVariable int id){
//        return preRegistrationService.putStudent(id);
//    }
//
//    @PutMapping("putStudents/{ids}")
//    @ApiOperation(value="推送学生到")
//    @ResponseBody
//    public R puhStudents(@PathVariable String ids){
//        Integer[] idArr = StringUtils.StringIdsToArrInt(ids);
//        return preRegistrationService.putStudentList(idArr);
//    }

    @PutMapping("billStudent/{id}")
    @ApiOperation(value="推送学生缴费")
    @ResponseBody
    @Logging(title = "推送缴费信息",describe = "推送缴费信息",type = BusinessType.QUERY)
    @RepeatSubmit
    public R billStudent(@PathVariable int id){
        return preRegistrationService.billStudent(id);
    }

//    @ApiOperation(value="推送缴费到博思")
//    @PutMapping("billStudent")
//    @ResponseBody
//    public R billStudent(HttpServletRequest request, HttpServletResponse response){
//        Enumeration<String> params = request.getParameterNames();
//        int id = Integer.parseInt(request.getParameter("id"));
//        Map<String,Object> parmentMap = new HashMap<>();
//        double moneyAll = 0;
//        while (params.hasMoreElements()){
//            String param = params.nextElement();
//            String value = request.getParameter(param);
//            double money = 0;
//            try{
//                money = Double.parseDouble(value);
//                if(money <= 0)
//                    throw new RuntimeException("请输入正确的金额");
//            }catch (Exception e){
//                return R.error("请输入正确的金额");
//            }
//            if(param.indexOf("3600200") >= 0){
//                moneyAll+=money;
//                parmentMap.put(param,money);
//            }
//        }
//        return preRegistrationService.billStudent(id,parmentMap,moneyAll);
//    }

    @ApiOperation(value="获取缴费信息")
    @GetMapping("reloadFeeStatus")
    @ResponseBody
    public R reloadFeeStatus(String billNo){
        return preRegistrationService.reloadFeeStatus(billNo);
    }


    @ApiOperation(value="批量更新缴费信息")
    @GetMapping("roledLogAll")
    @ResponseBody
    public R roledLogAll(){
        PreLog preLog = new PreLog();
        preLog.setStatus(0);
        List<PreLog> brandList = preLogService.selectList(preLog);

        for(PreLog preLog1 : brandList){
            try{
                preRegistrationService.reloadFeeStatus(preLog1.getBillNo());
            }catch (Exception e){
            }
        }
        return R.ok("更新成功");
    }


    @ApiOperation(value="删除账单")
    @GetMapping("delFeeStatus")
    @ResponseBody
    public R delFeeStatus(String billNo){
        return preRegistrationService.delFeeStatus(billNo);
    }

    @ApiOperation(value="取消账单")
    @GetMapping("cancelFree")
    @ResponseBody
    public R cancelFree(String billNo){
        return preRegistrationService.cancelFree(billNo);
    }

    /**
     * Describe: 获取修改视图
     * Param ModelAndView
     * Return 修改视图
     * */
    @GetMapping("edit")
    @ApiOperation(value="获取修改视图")
    public ModelAndView edit(ModelAndView modelAndView,int id){
        modelAndView.addObject("preRegistration",preRegistrationService.selectById(id));
        modelAndView.addObject("majorList",majorService.queryList(null));

        modelAndView.setViewName(MODULE_PATH + "edit");
        return modelAndView;
    }

    @PostMapping("update")
    @ApiOperation(value="修改数据")
    @ResponseBody
    public R update(@RequestBody PreRegistration preRegistration){
        int result = preRegistrationService.updateById(preRegistration);
        return R.resultRow(result);
    }


    @GetMapping("editByLog")
    @ApiOperation(value="获取修改页面有日志")
    public ModelAndView editByLog(ModelAndView modelAndView,int id){
        modelAndView.addObject("preRegistration",preRegistrationService.selectById(id));
        modelAndView.addObject("majorList",majorService.queryList(null));

        modelAndView.setViewName(MODULE_PATH + "editByLog");
        return modelAndView;
    }

    @PostMapping("updateByLog")
    @ApiOperation(value="修改数据添加日志")
    @ResponseBody
    public R updateByLog(@RequestBody PreRegistration preRegistration){
        int result = preRegistrationService.changeMajorByLog(preRegistration);
        return R.resultRow(result);
    }
}
